package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysModuleDAO;
import com.ourteam.system.dao.SysModule;
import com.ourteam.system.dao.SysModuleDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:37
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysModuleDAOJdbcImpl extends AbstractSpringDAO
    implements ISysModuleDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ModuleCode, ModuleName, ParentModuleId, Remarks, Status,
            DomainId, ModuleIcon, SortIndex, ModuleIconContent, ModuleType,
            DomainCode, DomainName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ModuleCode,
            _DataObjectProperty.ModuleName, _DataObjectProperty.ParentModuleId,
            _DataObjectProperty.Remarks, _DataObjectProperty.Status,
            _DataObjectProperty.DomainId, _DataObjectProperty.ModuleIcon,
            _DataObjectProperty.SortIndex, _DataObjectProperty.ModuleIconContent,
            _DataObjectProperty.ModuleType, _DataObjectProperty.DomainCode,
            _DataObjectProperty.DomainName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysModule, SysModuleDAOQueryBean> daoHelper;
    private com.ourteam.system.dao.impl.AbstractSysRoleModuleRelateDAOJdbcImpl SysRoleModuleRelateDao;

    public AbstractSysModuleDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysModule, SysModuleDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDomainDAO.EntityName)
                                .append(" SYS_DOMAIN ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDomainDAO.Id,
                    com.ourteam.system.dao.ISysDomainDAO.EntityName + ".",
                    "SYS_DOMAIN."));
            relatedJoinSQLBuffer.append(" = ").append(DomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysModule
         * @throws Exception
         */
    public boolean checkIsUnique(SysModule aSysModule)
        throws Exception {
        if (aSysModule == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysModule.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysModule
     * @throws Exception
     */
    public void insert(SysModule aSysModule) throws Exception {
        super.doInsert(aSysModule);
    }

    /**
         * Batch Insert Datas
         * @param aSysModule
         * @throws Exception
         */
    public void batchInsert(SysModule[] aSysModules) throws Exception {
        super.doBatchInsert(aSysModules);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysModuleDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysModule aSysModule) throws Exception {
        delete(aSysModule.getId());
    }

    /**
     * Update Data Selective
     * @param aSysModule
     * @throws Exception
     */
    public void update(SysModule aSysModule) throws Exception {
        if (aSysModule.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysModule);

        SysModule tempObj = findById(aSysModule.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysModule, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysModules[]
     * @throws Exception
     */
    public void batchUpdate(SysModule[] aSysModules) throws Exception {
        super.doBatchUpdate(aSysModules);
    }

    /**
         * Update Data By Condictions
         * @param aSysModule
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysModule aSysModule, SysModuleDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aSysModule, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysModuleDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysModule aSysModule)
        throws Exception {
        if (aSysModule.getModuleIconContent() == null) {
            throw new Exception(
                "The SysModule.moduleIconContent value is not allow NULL");
        }

        if (aSysModule.getModuleType() == null) {
            throw new Exception(
                "The SysModule.moduleType value is not allow NULL");
        }

        if (aSysModule.getDomainCode() == null) {
            throw new Exception(
                "The SysModule.domainCode value is not allow NULL");
        }

        if (aSysModule.getDomainName() == null) {
            throw new Exception(
                "The SysModule.domainName value is not allow NULL");
        }

        if (aSysModule.getModuleCode() == null) {
            throw new Exception(
                "The SysModule.moduleCode value is not allow NULL");
        }

        if (aSysModule.getModuleName() == null) {
            throw new Exception(
                "The SysModule.moduleName value is not allow NULL");
        }

        if (aSysModule.getRemarks() == null) {
            throw new Exception("The SysModule.remarks value is not allow NULL");
        }

        if (aSysModule.getStatus() == null) {
            throw new Exception("The SysModule.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysModule findById(final long id) throws Exception {
        SysModuleDAOQueryBean queryBean = new SysModuleDAOQueryBean();

        SysModuleDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysModule[] SysModules = query(queryBean);

        if (SysModules.length == 0) {
            return null;
        } else {
            return SysModules[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysModule[]
         * @throws Exception
         */
    public SysModule[] query(SysModuleDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysModuleRowMapper());

        return (SysModule[]) result.toArray(new SysModule[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(SysModuleDAOQueryBean queryBean)
        throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysModuleDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysModule[] aSysModules,
        SysModuleDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysModules, queryBean);
    }

    public void synchronize(SysModule[] aSysModules,
        SysModuleDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysModule> callBack)
        throws Exception {
        daoHelper.synchronize(aSysModules, queryBean, callBack);
    }

    public void setSysRoleModuleRelateDao(
        com.ourteam.system.dao.ISysRoleModuleRelateDAO SysRoleModuleRelateDao) {
        this.SysRoleModuleRelateDao = (com.ourteam.system.dao.impl.AbstractSysRoleModuleRelateDAOJdbcImpl) SysRoleModuleRelateDao;
    }

    /**
     * Query Related SysRoleModuleRelate
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysRoleModuleRelate[] querySysRoleModuleRelate(
        SysModuleDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleModuleRelateDAO.ModuleId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.EntityName +
            " ref_SysRoleModuleRelate";

        String[] entityFields = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.ENTITY_PROPERTIES;

        List list = SysRoleModuleRelateDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.JdbcSysRoleModuleRelateRowMapper());

        return (com.ourteam.system.dao.SysRoleModuleRelate[]) list.toArray(new com.ourteam.system.dao.SysRoleModuleRelate[list.size()]);
    }

    /**
     * Query Related SysRoleModuleRelate Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysRoleModuleRelateSelective(
        SysModuleDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleModuleRelateDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleModuleRelateDAO.ModuleId,
                "moduleId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleModuleRelateDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleModuleRelateDAO.RoleId,
                "roleId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleModuleRelateDAO.Status,
                "status");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleModuleRelateDAO.ModuleId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.EntityName +
            " ref_SysRoleModuleRelate";

        return SysRoleModuleRelateDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related SysRoleModuleRelate
     * @param aSysRoleModuleRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysRoleModuleRelate(
        com.ourteam.system.dao.SysRoleModuleRelate aSysRoleModuleRelate,
        SysModuleDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleModuleRelateDAO.ModuleId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.EntityName +
            " ref_SysRoleModuleRelate";

        String[] entityFields = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.ENTITY_PROPERTIES;

        SysRoleModuleRelateDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysRoleModuleRelate,
            aQueryBean);
    }

    /**
     * Delete Related SysRoleModuleRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysRoleModuleRelate(SysModuleDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleModuleRelateDAO.ModuleId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleModuleRelateDAO.EntityName; // + " ref_SysRoleModuleRelate";

        SysRoleModuleRelateDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcSysModuleRowMapper implements RowMapper<SysModule> {
        public SysModule mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysModule object = new SysModule();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object moduleCode = null;

                moduleCode = resultSet.getObject(columnIndex++);

                object.setModuleCodeNull(resultSet.getBoolean(columnIndex++));

                if (moduleCode != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleCode,
                        moduleCode);
                }

                Object moduleName = null;

                moduleName = resultSet.getObject(columnIndex++);

                object.setModuleNameNull(resultSet.getBoolean(columnIndex++));

                if (moduleName != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleName,
                        moduleName);
                }

                Object parentModuleId = null;

                parentModuleId = resultSet.getObject(columnIndex++);

                object.setParentModuleIdNull(resultSet.getBoolean(columnIndex++));

                if (parentModuleId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentModuleId,
                        parentModuleId);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object domainId = null;

                domainId = resultSet.getObject(columnIndex++);

                object.setDomainIdNull(resultSet.getBoolean(columnIndex++));

                if (domainId != null) {
                    setBeanValue(object, _DataObjectProperty.DomainId, domainId);
                }

                Object moduleIcon = null;

                moduleIcon = resultSet.getObject(columnIndex++);

                object.setModuleIconNull(resultSet.getBoolean(columnIndex++));

                if (moduleIcon != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleIcon,
                        moduleIcon);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                Object moduleIconContent = null;

                moduleIconContent = resultSet.getObject(columnIndex++);

                object.setModuleIconContentNull(resultSet.getBoolean(
                        columnIndex++));

                if (moduleIconContent != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleIconContent,
                        moduleIconContent);
                }

                Object moduleType = null;

                moduleType = resultSet.getObject(columnIndex++);

                object.setModuleTypeNull(resultSet.getBoolean(columnIndex++));

                if (moduleType != null) {
                    setBeanValue(object, _DataObjectProperty.ModuleType,
                        moduleType);
                }

                Object domainCode = null;

                domainCode = resultSet.getObject(columnIndex++);

                object.setDomainCodeNull(resultSet.getBoolean(columnIndex++));

                if (domainCode != null) {
                    setBeanValue(object, _DataObjectProperty.DomainCode,
                        domainCode);
                }

                Object domainName = null;

                domainName = resultSet.getObject(columnIndex++);

                object.setDomainNameNull(resultSet.getBoolean(columnIndex++));

                if (domainName != null) {
                    setBeanValue(object, _DataObjectProperty.DomainName,
                        domainName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
